package com.bjpowernode.web;

import com.bjpowernode.model.TActivity;
import com.bjpowernode.query.ActivityQuery;
import com.bjpowernode.result.R;
import com.bjpowernode.service.ActivityService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
public class ActivityController {

    @Resource
    private ActivityService activityService;

    /**
     * 编辑市场活动信息
     * @param activityQuery activityQuery
     * @param token token
     * @return R
     */
    @PutMapping(value = "/api/activity")
    public R editActivity(ActivityQuery activityQuery, @RequestHeader("Authorization")String token) {
        activityQuery.setToken(token);
        int update = activityService.updateActivity(activityQuery);
        return update >= 1 ? R.OK(): R.FAIL();
    }

    /**
     * 根据Id查询市场活动信息
     * @param id id
     * @return R
     */
    @GetMapping(value = "/api/activity/{id}")
    public R loadActivity(@PathVariable("id") Integer id) {
        TActivity tActivity = activityService.getActivityById(id);
        return R.OK(tActivity);
    }

    /**
     * 录入市场活动
     * @param activityQuery activityQuery
     * @param token token
     * @return R
     */
    @PostMapping(value = "/api/activity")
    public R addActivity(ActivityQuery activityQuery, @RequestHeader(value = "Authorization") String token) {
        activityQuery.setToken(token);
        int save = activityService.addActivity(activityQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 市场活动分页查询
     * @param current 当前页
     * @return R
     */
    @GetMapping(value = "/api/activity")
    public R activityPage(@RequestParam(value = "current", required = false)Integer current, ActivityQuery activityQuery) {
        if (current == null) {
            current = 1;
        }
        PageInfo<TActivity> pageInfo = activityService.getActivityByPage(current, activityQuery);
        return R.OK(pageInfo);
    }

}
